//
//  FQDMySubscriptionTitleBar.m
//  FakeQDaily
//
//  Created by mewxiv on 9/19/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMySubscriptionTitleBar.h"
#import "NSDate+FQDExtension.h"
#import "FQDRestModel.h"

#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface FQDMySubscriptionTitleBar ()

@property (nonatomic, strong) UIImageView *columnImageView;
@property (nonatomic, strong) UILabel *columnNameLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIButton *titleButton;

@end

@implementation FQDMySubscriptionTitleBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _columnImageView = [[UIImageView alloc] init];
    _columnImageView.contentMode = UIViewContentModeScaleAspectFill;
    _columnImageView.clipsToBounds = YES;
    _columnImageView.layer.cornerRadius = 12.0f;
    [self addSubview:_columnImageView];
    
    _columnNameLabel = [[UILabel alloc] init];
    _columnNameLabel.font = FQDBoldFontWithSize(11.0f);
    _columnNameLabel.textColor = [UIColor blackColor];
    [self addSubview:_columnNameLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = RGB(115, 115, 115);
    [self addSubview:_dateLabel];
    
    _titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_titleButton];
}

- (void)prepareConstraints
{
    WeakSelf;
    [_columnImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(10);
        make.centerY.equalTo(weakSelf);
        make.size.equalTo(@24);
    }];
    
    [_columnNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.columnImageView.mas_right).offset(8);
        make.centerY.equalTo(weakSelf);
        make.right.lessThanOrEqualTo(weakSelf.dateLabel.mas_left).offset(-10);
        make.height.equalTo(weakSelf);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf).offset(-10);
        make.centerY.equalTo(weakSelf.columnNameLabel);
        make.height.equalTo(weakSelf);
    }];
    
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf);
        make.height.equalTo(weakSelf);
        make.right.equalTo(weakSelf.dateLabel.mas_left);
    }];
}

- (void)subscribeColumn:(id)sender
{
    
}

- (void)setPost:(FQDPost *)post
{
    _post = post;
    
    FQDColumn *column = post.column;
    self.columnNameLabel.text = column.name;
    
    NSURL *columnImageURL = [NSURL URLWithString:column.image];
    [self.columnImageView sd_setImageWithURL:columnImageURL];
    
    self.dateLabel.text = [[NSDate dateWithTimeIntervalSince1970:post.publish_time] dateStringSinceNow];
    
    [self.titleButton addTarget:self action:@selector(subscribeColumn:) forControlEvents:UIControlEventTouchUpInside];
}

@end
